package com.employee.seckill.service;

import com.employee.seckill.entity.Employee;
import com.employee.seckill.mapper.EmployeeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: EmployeeServiceImpl
 * Package: com.employee.seckill.service
 * Description:
 *
 * @Author 刘荟雅
 * @Create 2025/9/10 19:22
 * @Version 1.0
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    private final EmployeeMapper employeeMapper;

    public EmployeeServiceImpl(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.listAll();
    }
    //姓名模糊查询 + 部门精确查询
    @Override
    public PageInfo<Employee> listByCondition(String name, String dept,
                                              int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeMapper.listByCondition(name, dept);
        return new PageInfo<>(list);
    }
    @Override
    public Employee findById(Long id) {
//        return employeeMapper.findById(id);
        //让查不到时主动抛异常
        Employee emp = employeeMapper.findById(id);
        if (emp == null) {
            throw new RuntimeException("No employee with id " + id);
        }
        return emp;
    }

    @Override
    public void add(Employee emp) {
        employeeMapper.insert(emp);
    }

    @Override
    public void update(Employee emp) {
        employeeMapper.update(emp);
    }

    @Override
    public void delete(Long id) {
        employeeMapper.delete(id);
    }
}